﻿using System.ComponentModel;
using EDMDesigner.Business.CSDL.Type.Entity;
using EDMDesigner.Business.CSDL.Util;
using System.Collections.Generic;
using EDMDesigner.Business.Util;

namespace EDMDesigner.Business.CSDL.Function
{
    public class Function : INotifyPropertyChanged, INamedElement
    {
        public Function()
        {
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set 
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private Visibility _visibility;
        public Visibility Visibility
        {
            get { return _visibility; }
            set 
            {
                _visibility = value;
                OnPropertyChanged("Visibility");
            }
        }

        private EntityType _entityType;
        public EntityType EntityType
        {
            get { return _entityType; }
            set 
            {
                _entityType = value;
                OnPropertyChanged("EntityType");
            }
        }

        private Property.Scalar.PropertyType? _scalarReturnType;
        public Property.Scalar.PropertyType? ScalarReturnType
        {
            get { return _scalarReturnType; }
            set 
            {
                _scalarReturnType = value;
                OnPropertyChanged("ScalarReturnType");
            }
        }

        public string ReturnType
        {
            get
            {
                if (EntityType == null)
                {
                    if (ScalarReturnType == null)
                        return null;
                    return string.Format("Collection({0})", ScalarReturnType.Value.ToString());
                }
                return string.Format("Collection({0}.{1})", EntityType.Container.Namespace,  EntityType.Name);
            }
        }

        private EventedObservableCollection<FunctionParameter> _parameters;
        public EventedObservableCollection<FunctionParameter> Parameters
        {
            get
            {
                if (_parameters == null)
                    _parameters = new EventedObservableCollection<FunctionParameter>();
                return _parameters;
            }
        }

        public SSDL.Function.Function SSDLFunction { get; set; }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
